#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//同一条链上no

#define MAXN 40001

typedef struct Node
{
    int pre;//前一个结点
    int lenth;//到前一个结点的距离
    int linked;//是否被链接过
}Node;

int main()
{
    int t = 0;
    int n = 0, m = 0;
    int i = 0, j = 0, len;
    scanf("%d", &t);
    Node Tree[MAXN];

    while(t--)
    {
        memset(Tree, 0, sizeof(Tree));
        scanf("%d %d", &n, &m);

        while(--n)
        {
            scanf("%d %d %d", &i, &j, &len);
            if(Tree[i].pre != 0)
            {
                Tree[j].pre = i;
                Tree[j].lenth = len;
                Tree[i].linked++;
            }
            else if(Tree[j].pre != 0)
            {
                Tree[i].pre = j;
                Tree[i].lenth = len;
                Tree[j].linked++;
            }
            else
            {
                Tree[i].pre = j;
                Tree[i].lenth = len;
                Tree[j].linked++;
            }
        }

        int p1 = 0, p2 = 0;
        int sumlen = 0;
        while(m--)
        {
            sumlen = 0;
            scanf("%d %d", &p1, &p2);

            int p3 = p1, p4 = p2;
            //检查二者是否在同一路径上
            while(p3 != p2 && Tree[p3].pre != 0)
            {
                sumlen += Tree[p3].lenth;
                p3 = Tree[p3].pre;
            }
            if(p3 == p2)
            {
                printf("%d\n", sumlen);
                continue;
            }
            else
            sumlen = 0;

            while(p4 != p1 && Tree[p4].pre != 0)
            {
                sumlen += Tree[p4].lenth;
                p4 = Tree[p4].pre;
            }
            if(p4 == p1)
            {
                printf("%d\n", sumlen);
                continue;
            }
            else
            sumlen = 0;

            while(p1 != p2)
            {
                do
                {
                    sumlen += Tree[p1].lenth;
                    p1 = Tree[p1].pre;
                }while(Tree[p1].linked == 1);

                do
                {
                    sumlen += Tree[p2].lenth;
                    p2 = Tree[p2].pre;
                }while(Tree[p2].linked == 1);
            }
            printf("%d\n", sumlen);
        }
    }
    return 0;
}